package com.ostrich.shopping.ui.user.views;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.shopping.R;
import com.ostrich.shopping.base.BaseMvvmActivity;
import com.ostrich.shopping.databinding.ActivityLoginBinding;
import com.ostrich.shopping.net.BaseResponseEntity;
import com.ostrich.shopping.room.User;
import com.ostrich.shopping.ui.user.adapteer.UserAdapter;
import com.ostrich.shopping.ui.user.entity.UserEntity;
import com.ostrich.shopping.ui.user.viewmodel.LoginViewModel;
import com.ostrich.shopping.utils.DateTimeUtils;

import java.util.List;

public class LoginActivity extends BaseMvvmActivity<ActivityLoginBinding, LoginViewModel>{
    public static final int REQUEST_CODE = 101;
    @Override
    protected void iniView() {

        List<User> list = viewModel.getAll();

        UserAdapter userAdapter = new UserAdapter(this, list);
        binding.spinner.setAdapter(userAdapter);
        binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                binding.etUsername.setText(list.get(position).getNickname());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        binding.btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etUsername.getText().toString();
                String password = binding.etPassword.getText().toString();
                viewModel.login(username,password).observe(LoginActivity.this, new Observer<BaseResponseEntity<UserEntity>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<UserEntity> userEntityBaseResponseEntity) {
                        if(userEntityBaseResponseEntity.getCode() == 1 && !userEntityBaseResponseEntity.getMsg().equals("登录失败!")){
                            ToastUtils.showShort("登陆成功");


                            //存储当前用户
                            UserEntity data = userEntityBaseResponseEntity.getData();
                            User user = new User();
                            user.setAddress(data.getAddress());
                            user.setUserId(data.getUserId());
                            user.setNickname(data.getNickname());
                            user.setPhoneNumber(data.getPhoneNumber());
                            user.setHeadImage(data.getHeadImage());
                            user.setBirthday(data.getBirthday());
                            user.setRealname(data.getRealname());
                            user.setLoginTime(DateTimeUtils.getCurrTimeStr());//获取当前系统时间
                            viewModel.insert(user);

                        }else{
                            ToastUtils.showShort(userEntityBaseResponseEntity.getMsg());
                        }
                    }
                });

            }
        });

        binding.btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE && resultCode == RegisterActivity.RESULT_CODE){
            UserEntity user = (UserEntity) data.getSerializableExtra("user");
            binding.etUsername.setText(user.getNickname());
        }
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(LoginViewModel.class);
    }
}